<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Message;
use DB,Session;

class FeedBackController extends Controller
{
    /**
     * Display a listing of the resource.
     *显示问题列表
     * @return \Illuminate\Http\Response
     */
    public function index()
    { 
//        return "这是问题反馈首页";
        //查询问题信息
        $feedback = DB::table("feedback")
                ->where('fb_status', '<>', '-1')
                ->orderBy('id', 'desc')
                ->paginate(20);
        $status = [0=>'未处理', 1=>'已阅读',2=>'已处理'];
        //加载模板显示
        return view("admin.feedback.index", compact("feedback", 'status'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
//        return "添加反馈";
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //获取用户问题反馈的信息
        $rec = DB::table("feedback")->where("id", $id)->first();
        DB::table("feedback")->where("id", $id)->update(['fb_status' => 1]);
        //显示模板
        return view("admin.feedback.edit", compact("rec"));
    }

    /**
     * Update the specified resource in storage.
     *查看问题内容进行回复
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
//        dd($id);
        $data = $request->except("_token", "_method");
        $data['fb_status'] = 2;
        DB::table("feedback")->where("id", $id)->update($data);
        
        // 获取用户id
        $fb = DB::table('feedback')->find($id);
        $data = [
            'msg_title'=>'问题反馈处理通知',
            'msg_content'=>'您提交的' . $fb->fb_title . '已管理员被处理,处理结果：<br>' . $fb->re_content,
        ];
        $msg = new Message();
        $msg->autoSendMessage($fb->user_id,$data);
        // 发送系统消息给用户
        return redirect("/admin/feedback");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DB::table("feedback")->where("id", $id)->update(['fb_status' => -1]);
        return back();
    }
}
